package com.fudan.biz.visualize.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fudan.biz.visualize.service.AggregationService;
import com.fudan.es.aggs.entity.ChartResponseEntity;
import com.fudan.framework.entity.ResponseMsg;

@Controller
@RequestMapping("/biz/chart")
public class ChartDataController {


	@Autowired
	private AggregationService aggregationService;

	@RequestMapping("/draw")
	@ResponseBody
	public ResponseMsg test(HttpServletRequest request) {
		String req = request.getParameter("req");
		try {
				ChartResponseEntity chartResponseEntity = aggregationService.aggsResponse(req);
				System.out.println("==============================");
				System.out.println(chartResponseEntity.getResponseData());
				System.out.println("=============================");
				List<Map<String,Object>> flatMap = new ArrayList<>();
				aggregationService.flatData(flatMap,null,chartResponseEntity.getResponseData());
				System.out.println(flatMap);
				return ResponseMsg.build().putData(chartResponseEntity.getResponseData());
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMsg.build(500, "失败");
		}
	}

	@RequestMapping("/data")
	@ResponseBody
	public ResponseMsg data(HttpServletRequest request) {
		String req = request.getParameter("req");
		try {
				ChartResponseEntity chartResponseEntity = aggregationService.aggsResponse(req);
				return ResponseMsg.build().putData(chartResponseEntity);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseMsg.build(500, "失败");
		}
	}
}
